package com.duapk.web.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;

/**
 * 日志管理对象
 * 
 * @author yangfeng
 * 
 */
public class LogManager {

	public static void init(String logConfigFile) {
		System.out.println("Start LogBack Load ...");
		LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
		try {
			System.out.println("[LogBack] Config File is " + logConfigFile);
			JoranConfigurator configurator = new JoranConfigurator();
			configurator.setContext(lc);
			lc.reset();
			configurator.doConfigure(logConfigFile);
			System.out.println("[LogBack] Context Property 'Log_Home' is "
					+ lc.getProperty("Log_Home"));
		} catch (JoranException e) {
			e.printStackTrace();
			System.out
					.println("[FATAL] Logback Init Exception, System Exit !!!");
			System.exit(-1);
		}
		System.out.println("LogBack Load End!");
	}

	/**
	 * 访问日志
	 */
	private static Logger visitLogger = LoggerFactory.getLogger("visitLog");
	private static Logger searchLogger = LoggerFactory.getLogger("searchLog");
	private static Logger downloadLogger = LoggerFactory.getLogger("downloadLog");
	

	private static final char Seperator = '|';

	public static void searchLog(String client, String keyword, String type,String version,int pageid, int resultSize,
			String ip, String ua) {
		StringBuilder sb = new StringBuilder();
		sb.append(client).append(Seperator);
		sb.append(keyword).append(Seperator);
		sb.append(type).append(Seperator);
		sb.append(version).append(Seperator);
		sb.append(pageid).append(Seperator);
		sb.append(resultSize).append(Seperator);
		sb.append(ip).append(Seperator);
		sb.append(ua);
		searchLogger.warn(sb.toString());
	}
	
	public static void visitLog(String url,String name,String ip,String ua){
		StringBuilder sb = new StringBuilder();
		sb.append(url).append(Seperator);
		sb.append(name).append(Seperator);
		sb.append(ip).append(Seperator);
		sb.append(ua);
		visitLogger.warn(sb.toString());
	}
	
	public static void downloadLog(String appName,String ip,String ua){
		StringBuilder sb = new StringBuilder();
		sb.append(appName).append(Seperator);
		sb.append(ip).append(Seperator);
		sb.append(ua);
		downloadLogger.warn(sb.toString());
	}
}
